from django.contrib import admin

# Register your models here.
from .models import Grades, Students


class StudentsInfo(admin.TabularInline):  # 另一种形式admin.StackedInline
    model = Students
    extra = 2

@admin.register(Grades)
class GradeAdmin(admin.ModelAdmin):
    # 上面定义了一个类，在这使用
    inlines = [StudentsInfo]

    list_display = ["pk", "gname", "gdate", "ggirlnum", "gboynum", "isDelete"]
    list_filter = ["gname"]
    search_fields = ["gname"]
    list_per_page = 5
    # fields = ["ggirlnum", "gboynum", "gname", "gdate", "isDelete"]  # pk是自动生成的不能手动添加，所以加到修改页是会报错的
    # fieldsets，添加和修改时，把属性分成几个组
    fieldsets = [
        ("num", {"fields": ["ggirlnum", "gboynum"]}),
        ("base", {"fields": ["gname", "gdate", "isDelete"]})
    ]

@admin.register(Students)
class StudentAdmin(admin.ModelAdmin):
    # 修改性别列中值，显示字样
    def gender(self):
        if self.sgender:
            return "男"
        else:
            return "女"

    # 修改isDelete列中值，显示字样
    def isDelete(self):
        if self.isDelete:
            return "是"
        else:
            return "否"
    # 定义函数，用于修改表头
    def sname(self):
        return self.sname

    def sage(self):
        return self.sage

    # 修改表头为汉字，只有定义了上述函数，下面才能使用
    gender.short_description = "性别"
    isDelete.short_description = "是否删除"
    sname.short_description = "姓名"
    sage.short_description = "年龄"

    list_display = [sname, gender, sage, "scontend", "sgrade", isDelete]
    list_filter = ["sname"]
    search_fields = ["sname"]
    list_per_page = 2
    # fields = ["ggirlnum", "gboynum", "gname", "gdate", "isDelete"]  # pk是自动生成的不能手动添加，所以加到修改页是会报错的
    # fieldsets，添加和修改时，把属性分成几个组
    fieldsets = [
        ("num", {"fields": ["sname", "sgender"]}),
        ("base", {"fields": ["sage", "scontend", "sgrade"]})
    ]

    actions_on_bottom = True
    actions_on_top = False

# admin.site.register(Grades, GradeAdmin)
# admin.site.register(Students, StudentAdmin)
